﻿$(document).ready(function () {
    //    page.commonUserControl = new NDUAP.Selector({ divID: "userId", typeID: 1, totalSelectNum: 1, quickBoxPositionX: -285, width: 150, searchData: page.userList });
    $("#ul-nav li:not(.nav-header)").eq(4).addClass("active");
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/admin/suggest.ashx?action=",
    popU: null,

    init: function () {
        page.getList();
    },

    getList: function (pageIndex) {
        var deptName = $.trim($("#deptName").val());
        var relateDeptName = $.trim($("#relateDeptName").val());
        var status = $("#status").val();

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        $.get(page.url + "getList", { pageIndex: page.pageIndex, pageSize: page.pageSize, status: status, deptName: deptName, relateDeptName: relateDeptName, r: Math.random()
        }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("suggest", data));
            page.pager(data.count);
            page.list = data.list;
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.getList
        });
    },

    grant: function (id) {
        var html = [];
        html.push('<form class="form-horizontal">');
        html.push('<div class="control-group"><label class="control-label">类型</label>');
        html.push('<div class="controls"><select id="type"><option value="">请选择</option><option value="1">素币</option><option value="2">现金（单位：元），线下操作</option></select></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="amount">奖励数额</label>');
        html.push('<div class="controls"><input id="amount" placeholder="奖励数额" type="text" required/></div>');
        html.push('</div>');
        html.push('<div class="control-group">');
        html.push('<div class="controls"><button class="btn" type="button" onclick="page.grantSubmit(' + id + ')">确认奖励</button></div>');
        html.push('</div>');
        html.push('</form>');

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "奖励 <small class='muted'>奖励只能操作一次，不允许更改</small>",
            contents: html.join(""),
            w: 400,
            drag: true
        });
        page.popU.showPop();

        $("#amount").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");
    },

    grantSubmit: function (id) {
        var type = $("#type").val();
        var amount = $("#amount").val();

        if (type == "") {
            alert("请选择奖励类型");
            $("#amount").focus();
            return;
        }

        if ($.trim(amount) == "" || parseInt(amount) <= 0) {
            alert("请输入奖励数额");
            $("#amount").focus();
            return;
        }

        $.post(page.url + "grant", { id: id, type: type, amount: amount }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getList();
            page.popU.close();
        }, "json");
    },

    forward: function (id, title) {
        var obj = null;
        for (var i = 0; i < page.list.length; i++) {
            if (page.list[i].Id == id) {
                obj = page.list[i];
                break;
            }
        }

        if (obj == null) {
            alert("页面已过时，请刷新页面重新操作");
            return false;
        }

        var html = [];
        html.push('<form class="form-horizontal">');
        html.push('<div class="control-group"><label class="control-label" for="urelateDeptName">涉及部门</label>');
        html.push('<div class="controls"><input id="urelateDeptName" placeholder="涉及部门" value="' + obj.RelateDeptName + '" type="text" required/></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="amount">处理人</label>');
        html.push('<div class="controls"><span id="ToUser"></span></div>');
        html.push('</div>');
        html.push('<div class="control-group"><label class="control-label" for="ucontrolcomment">信息</label>');
        html.push('<div class="controls"><textarea id="ucontrolcomment" style="width:350px" rows="5"></textarea></div>');
        html.push('</div>');
        html.push('<div class="control-group">');
        html.push('<div class="controls"><button class="btn" type="button" onclick="page.forwardCommit(' + id + ')">提交</button></div>');
        html.push('</div>');
        html.push('</form>');

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: title != undefined ? title : "转他人处理",
            contents: html.join(""),
            w: 600,
            drag: true
        });
        page.popU.showPop();

        page.forwardControl = new NDUAP.Selector({ divID: "ToUser", typeID: 1, totalSelectNum: 1, quickBoxPositionX: -285, width: 150, searchData: page.userList });

        if (obj.ControlComment != "") {
            $("#ucontrolcomment").val(obj.ControlComment);
        } else {
            $("#ucontrolcomment").val(obj.Description);
        }

        if (obj.ToUserId != 0) {
            for (var i = 0; i < page.userList.length; i++) {
                if (page.userList[i].name == obj.ToUserName + "(" + obj.ToUserId + ")") {
                    page.forwardControl.setValue([page.userList[i]]);
                }
            }
        }
    },

    forwardCommit: function (id) {
        var toUser = page.forwardControl.getValue().length > 0 ? page.forwardControl.getValue()[0] : null;
        if (toUser == null) {
            alert("请选择处理人");
            return false;
        }

        var relateDeptName = $.trim($("#urelateDeptName").val());
        var controlComment = $.trim($("#ucontrolcomment").val());

        if (relateDeptName == "") {
            alert("涉及部门不能为空");
            $("#relateDeptName").focus();
            return false;
        }

        $.post(page.url + "forward", { id: id, toUserId: toUser.id, relateDeptName: relateDeptName, controlComment: controlComment }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.getList();
            page.popU.close();
        }, "json");
    },

    closeSuggest: function (id) {
        if (confirm("确定把当前建议置为完成么？")) {
            $.post(page.url + "close", { id: id }, function (data) {
                if (data.error) {
                    alert(data.error);
                    return false;
                }
                page.getList();
                page.popU.close();
            }, "json");
        }
    }
};

